//
//  TourismFirstVC.m
//  Buiom_7.0
//
//  Created by lanou3g on 15/10/28.
//  Copyright © 2015年 triffe. All rights reserved.
//

#import "TourismFirstVC.h"
#import "ToursimFirstCell.h"
#import "UIImageView+WebCache.h"
#import "ToursimSecondVC.h"
#import "ToursimFirstModel.h"
#import "SuperToursimVC.h"

#define     KsWidth  [UIScreen mainScreen].bounds.size.width
#define     KsHeigh  [UIScreen mainScreen].bounds.size.height

@interface TourismFirstVC ()<UITableViewDataSource, UITableViewDelegate,MONActivityIndicatorViewDelegate>

@property (nonatomic, strong) UITableView *tableView;
// 城市名装进数组中
@property (nonatomic, strong) NSMutableArray *cityNameArray;
// 将城市对应的图片装进数组中
@property (nonatomic, strong) NSMutableArray *cityPictureArray;
@property (nonatomic, strong) ToursimFirstModel *model;

@property (nonatomic, strong) NSMutableArray *modelArray;
@property (nonatomic, strong) UINavigationBar *navagationBar;
// 声明一个等待图片加载的方法的属性
@property (nonatomic, strong) MONActivityIndicatorView *indicationView;



@end

@implementation TourismFirstVC

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view from its nib.
    // 即是否根据按所在界面的navigationbar与tabbar的高度，自动调整scrollview的 inset,设置为no，让它不要自动调整就可以了
    self.automaticallyAdjustsScrollViewInsets = NO;
    // 加载视图
    [self addSubView];
    // 隐藏tableview的分割线
    self.tableView.separatorStyle = NO;
    // 加载一个等待动画
    self.indicationView = [[MONActivityIndicatorView alloc] init];
    self.indicationView.center = self.tableView.center;
    CGPoint point = self.indicationView.center;
    point.y = self.tableView.center.y - 200;
    self.indicationView.center = point;
    self.indicationView.delegate = self;
    [self.tableView addSubview:self.indicationView];
    [self.indicationView startAnimating];
    
    // 加载数据
    [self addUpData];
   
}
// 实现等待图片的加载动画的代理方法
- (UIColor *)activityIndicatorView:(MONActivityIndicatorView *)activityIndicatorView circleBackgroundColorAtIndex:(NSUInteger)index
{
    if (index == 0) {
        return [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
    } if (index == 1) {
        return [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
    } if (index == 2) {
        return [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
    } if (index == 3) {
        return [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
    }
    
    return [UIColor colorWithRed:arc4random() % 256 / 255.0 green:arc4random() % 256 / 255.0 blue:arc4random() % 256 / 255.0 alpha:1];
}

// 加载视图
- (void)addSubView
{
    self.navagationBar = [[UINavigationBar alloc] initWithFrame:CGRectMake(0, 0, KsWidth, 64)];
    self.navagationBar.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"toursimBarBackcolor"]];
    [self.view addSubview:_navagationBar];
    self.navigationItem.title = @"旅游国家";
    
//    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithTitle:@"返回主页" style:(UIBarButtonItemStylePlain) target:self action:@selector(leftBarButton)];
    UIBarButtonItem *left = [[UIBarButtonItem alloc] initWithImage:[UIImage imageNamed:@"houtuiyibu"] style:(UIBarButtonItemStylePlain) target:self action:@selector(leftBarButton)];
    
    self.navigationItem.leftBarButtonItem = left;
    
    UIBarButtonItem *right = [[UIBarButtonItem alloc] initWithTitle:@"特色类型" style:(UIBarButtonItemStylePlain) target:self action:@selector(rightBarButton)];
    self.navigationItem.rightBarButtonItem = right;
    
    NSArray *titleArray = @[@"热门地", @"欧洲", @"港澳台", @"优质区", @"亚洲", @"国内"];
    UISegmentedControl *segment = [[UISegmentedControl alloc] initWithItems:titleArray];
    segment.frame = CGRectMake(0, 64, KsWidth, 36);
    
   segment.backgroundColor = [UIColor colorWithPatternImage:[UIImage imageNamed:@"toursimBarBackcolor"]];
    segment.selectedSegmentIndex = 5;
  //  segment.tintColor = [UIColor purpleColor];
    
    [segment addTarget:self action:@selector(segmentControllerAction:) forControlEvents:(UIControlEventValueChanged)];
    [self.view addSubview:segment];
    
    self.tableView = [[UITableView alloc] initWithFrame:CGRectMake(0, 100, KsWidth, KsHeigh - 100) style:(UITableViewStylePlain)];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;

    self.tableView.bounces = NO;
    self.tableView.showsVerticalScrollIndicator = NO;

    

    [self.view addSubview:_tableView];
   
}
// 两个barbutton的方法设置
- (void)leftBarButton
{
    [self dismissViewControllerAnimated:YES completion:nil];
    
}

- (void)rightBarButton
{
    SuperToursimVC *superVC  = [[SuperToursimVC alloc] init];
    [self.navigationController pushViewController:superVC animated:YES];
}

// 返回行数
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.cityPictureArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    NSString *identifier = @"dfasfdf";
    ToursimFirstCell *cell = [tableView dequeueReusableCellWithIdentifier:identifier];
    if (cell == nil) {
        cell = [[ToursimFirstCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:identifier];
    }
    NSString *string = self.cityPictureArray[indexPath.row];
    NSURL *url = [NSURL URLWithString:string];
    [cell.imageViews sd_setImageWithURL:url];
    cell.label.textColor = [UIColor whiteColor];
    cell.label.text = self.cityNameArray[indexPath.row];

    [self.indicationView stopAnimating];

    
    return cell;
}

- (void)segmentControllerAction:(UISegmentedControl *)segment
{
    switch (segment.selectedSegmentIndex) {
        case 0:
            self.pagesCount = segment.selectedSegmentIndex + 2;
            // 加载数据
            [self addUpData];
            
            // 刷新页面(刷新表格)
            [self.tableView reloadData];
              break;
          case 1:
            self.pagesCount = segment.selectedSegmentIndex + 2;
            // 加载数据
            [self addUpData];
            // 刷新页面(刷新表格)
            [self.tableView reloadData];
            break;
            
        case 2:
            self.pagesCount = segment.selectedSegmentIndex + 2;
            // 加载数据
            [self addUpData];
            // 刷新页面(刷新表格)
            [self.tableView reloadData];
            break;
        case 3:
            self.pagesCount = segment.selectedSegmentIndex + 2;
            // 加载数据
            [self addUpData];
            // 刷新页面(刷新表格)
            [self.tableView reloadData];
            break;
        case 4:
            self.pagesCount = segment.selectedSegmentIndex + 2;
            // 加载数据
            [self addUpData];
            // 刷新页面(刷新表格)
            [self.tableView reloadData];
            break;
        case 5:
            self.pagesCount = segment.selectedSegmentIndex + 3;
            // 加载数据
            [self addUpData];
            // 刷新页面(刷新表格)
            [self.tableView reloadData];
            break;
            
        default:
            break;
    }
}
// 行高
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return 200;
}
// 点击方法,点击之后实现跳转
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    ToursimSecondVC *tSecondVC = [[ToursimSecondVC alloc] init];
    UINavigationController *navC = [[UINavigationController alloc] initWithRootViewController:tSecondVC];
    ToursimFirstModel *model = self.modelArray[indexPath.row];
    tSecondVC.type = model.type;
    tSecondVC.ID = model.IDS;
    
    
     [self presentViewController:navC animated:YES completion:nil];
   // [self.navigationController pushViewController:tSecondVC animated:YES];
}

// 加载数据
- (void)addUpData
{
    if (self.pagesCount == 0) {
        _pagesCount = 8;
        
    }
  
    NSLog(@"%ld", self.pagesCount);
    [self.cityPictureArray removeAllObjects];
    [self.cityNameArray removeAllObjects];
    [self.modelArray removeAllObjects];
    NSString *urlString =[NSString stringWithFormat: @"http://api.breadtrip.com/destination/index_places/%ld/", (long)self.pagesCount];
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    // 数据请求
    [NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse * _Nullable response, NSData * _Nullable data, NSError * _Nullable connectionError) {
        // 进行数据解析
        
        NSDictionary *dic = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];

        NSArray *array = dic[@"data"];
       
        for (NSDictionary *dic2 in array) {
            [self.cityNameArray addObject:dic2[@"name"]];
            [self.cityPictureArray addObject:dic2[@"cover"]];

            ToursimFirstModel *model = [[ToursimFirstModel alloc] init];
//             [model setValuesForKeysWithDictionary:dic2];
            model.type = dic2[@"type"];
            model.IDS = dic2[@"id"];
            
            [self.modelArray addObject:model];
           
            
        }
        
        [self.tableView reloadData];
        
    }];
    
  
}

- (NSMutableArray *)modelArray
{
    if (_modelArray == nil) {
        _modelArray = [NSMutableArray array];
        
    }
    return _modelArray;
}

-(NSMutableArray *)cityNameArray
{
    if (_cityNameArray == nil) {
        _cityNameArray = [NSMutableArray array];
    }
    return _cityNameArray;
}

-(NSMutableArray *)cityPictureArray
{
    if (_cityPictureArray == nil) {
        _cityPictureArray = [NSMutableArray array];
    }
    return _cityPictureArray;
}
// model的懒加载方法
- (ToursimFirstModel *)model
{
    if (_model == nil) {
        _model = [[ToursimFirstModel alloc] init];
    }
    return _model;
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
